package com.ruoyi.web.controller.erp;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.common.enums.ProductType;
import com.ruoyi.erp.domain.XzgCustomer;
import com.ruoyi.erp.domain.XzgProduct;
import com.ruoyi.erp.service.IXzgCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author qiuwencong
 * @Date 2021/12/2 9:19
 * @Package com.ruoyi.web.controller.erp
 * @Description
 */
@RestController
@RequestMapping("/erp/customer")
public class CustomerController extends BaseController {

    @Autowired
    private IXzgCustomerService customerService;

    /**
     * 根据id获取客户信息
     *
     * @param customerId
     * @return
     */
    @GetMapping("/{customerId}")
    public AjaxResult getCustomer(@PathVariable Long customerId) {
        return AjaxResult.success(customerService.selectCustomerById(customerId));
    }

    /**
     * 增加用户信息
     *
     * @param customer
     * @return
     */
    @PostMapping
    public AjaxResult addCustomer(@Validated @RequestBody XzgCustomer customer) {
        customer.setCreateBy(getUsername());
        customer.setUpdateBy(getUsername());
        customer.setCreateTime(new Date());
        customer.setUpdateTime(new Date());
        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 更新客户信息
     *
     * @param customer
     * @return
     */
    @PutMapping
    public AjaxResult updateCustomer(@Validated @RequestBody XzgCustomer customer) {
        customer.setUpdateBy(getUsername());
        customer.setUpdateTime(new Date());
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 分页获取客户信息
     *
     * @param customer
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(XzgCustomer customer) {
        startPage();
        List<XzgCustomer> customers = customerService.selectCustomerList(customer);
        return getDataTable(customers);
    }

    /**
     * 逻辑删除客户信息
     *
     * @param customerIds
     * @return
     */
    @DeleteMapping("/{customerIds}")
    public AjaxResult deleteCustomer(@PathVariable Long[] customerIds) {
        return toAjax(customerService.deleteCustomerById(customerIds, getUsername()));
    }

    /**
     * 获取客户名字
     *
     * @return
     */
    @GetMapping("/names")
    public AjaxResult getCustomerNames() {
        return AjaxResult.success(customerService.selectCustomerNames());
    }

    /**
     * 导出
     *
     * @param response
     * @param customer
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, XzgCustomer customer) {
        List<XzgCustomer> customers = customerService.selectCustomerList(customer);
        ExcelUtil<XzgCustomer> util = new ExcelUtil<>(XzgCustomer.class);
        util.exportExcel(response, customers, "产品数据");
    }
}
